<?php
namespace app\erp_meixingtiyu\model;

use think\Model;
use think\model\concern\SoftDelete;
use app\common\model\UploadFile;
use app\common\model\User;
use app\common\enum\order\PayStatus as OrderPayStatusEnum;

class MemberCardOrder extends Model
{
    use SoftDelete;
    protected $deleteTime = 'delete_time';
    protected $table = 'athlon_membercard_order';
    protected $pk = 'order_id';

    public function image()
    {
        return $this->hasOne(UploadFile::class, 'file_id', 'image_id');
    }

    public function user()
    {
      return $this->hasOne(User::class, 'user_id', 'user_id');
    }

    public function getPayTimeStrAttr($value, $data)
    {
        return date('Y-m-d H:i:s', $data['pay_time']); 
    }

    public function getList()
    {
        return $this->with(['image', 'user'])
            ->append(['pay_time_str'])
            ->where('pay_status', 20)
            ->order(['order_id' => 'asc'])
            ->paginate(15);
    }

    public function getListByDate($startDate, $endDate)
    {
        return $this->with(['image', 'user'])
            ->append(['pay_time_str'])
            ->where('pay_status', 20)
            ->whereBetweenTime('pay_time', $startDate, $endDate)->order('pay_time desc')
            ->select();
    }

    public function getOrdersByDate($date)
    {
        return $this->where('pay_status', OrderPayStatusEnum::SUCCESS)->whereDay('pay_time', $date)->select();
    }
}
